/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2023-02-07     HZHJYYJQR       the first version
 */

#include "app_nfc.h"
#include <Bsp/bsp_uart2.h>
#include <HJComm/HJRfCard.h>
#define DBG_TAG "app_nfc"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>

NFC m_nfc;

//寻卡
void nfc_findcard_send(void)
{
    uint8_t buf[16]={0};
    buf[0]=0xaa;
    buf[1]=0xbb;
    buf[2]=0x06;
    buf[6]=FindCardCmd>>8;
    buf[7]=(uint8_t)FindCardCmd;
    buf[8]=0x52;
    buf[9]=buf[4]^buf[5]^buf[6]^buf[7]^buf[8];

    bsp_uart2_tx_transmit(buf,10);
}
//防冲突
void nfc_AntiCollisionCmd_send(uint8_t* buff,uint8_t buf_len)
{
    uint8_t buf[16]={0};
    uint8_t sum_xor=0;
    buf[0]=0xaa;
    buf[1]=0xbb;
    buf[2]=0x05+buf_len;
    buf[6]=AntiCollisionCmd>>8;
    buf[7]=(uint8_t)AntiCollisionCmd;
    sum_xor= sum_xor^buf[4]^buf[5]^buf[6]^buf[7];
    for(uint8_t i =0;i<buf_len;i++){
        buf[8+i]=buff[i];
        if(buff[i]==0xaa)buf[2]--;
        sum_xor=sum_xor^buf[8+i];
    }
    buf[8+buf_len]=sum_xor;

    bsp_uart2_tx_transmit(buf,9+buf_len);
}

//选卡
void nfc_SelectCardCmd_send(uint8_t* buff,uint8_t buf_len)
{
    uint8_t buf[16]={0};
    uint8_t sum_xor=0;
    buf[0]=0xaa;
    buf[1]=0xbb;
    buf[2]=0x05+buf_len;
    buf[6]=SelectCardCmd>>8;
    buf[7]=(uint8_t)SelectCardCmd;
    sum_xor= sum_xor^buf[4]^buf[5]^buf[6]^buf[7];
    for(uint8_t i =0;i<buf_len;i++){
        buf[8+i]=buff[i];
        if(buff[i]==0xaa)buf[2]--;
        sum_xor=sum_xor^buf[8+i];
    }
    buf[8+buf_len]=sum_xor;

    bsp_uart2_tx_transmit(buf,9+buf_len);
}
//认证
void nfc_CardCertificateCmd_send(uint8_t passwordtype,uint8_t blocknum)
{
    uint8_t buf[16]={0};
    uint8_t sum_xor=0;
    buf[0]=0xaa;
    buf[1]=0xbb;
    buf[2]=0x0D;
    buf[6]=attestateCmd>>8;
    buf[7]=(uint8_t)attestateCmd;
    buf[8]=passwordtype;
    buf[9]=blocknum;
    sum_xor= sum_xor^buf[4]^buf[5]^buf[6]^buf[7]^buf[8]^buf[9];
    for(uint8_t i =0;i<6;i++){
        buf[10+i]=0xff;
        sum_xor=sum_xor^buf[10+i];
    }
    buf[16]=sum_xor;

    bsp_uart2_tx_transmit(buf,17);
}

//一键读卡
void nfc_OneKeyReadCardCmd_send(uint8_t blocknum)
{
    uint8_t buf[16]={0};
    buf[0]=0xaa;
    buf[1]=0xbb;
    buf[2]=0x06;
    buf[6]=ReadCardCmd>>8;
    buf[7]=(uint8_t)ReadCardCmd;
    buf[8]=blocknum;
    buf[9]=buf[4]^buf[5]^buf[6]^buf[7]^buf[8];
    bsp_uart2_tx_transmit(buf,10);
}

//一键写卡
void nfc_OneKeyWriteCardCmd_send(uint32_t m_total,uint32_t m_remain,uint8_t blocknum)
{
    uint8_t buf[16]={0};
    uint8_t sum_xor=0;
    buf[0]=0xaa;
    buf[1]=0xbb;
    buf[2]=0x16;
    buf[6]=WriteCardCmd>>8;
    buf[7]=(uint8_t)WriteCardCmd;
    buf[8]=blocknum;

    buf[9]= (uint8_t)m_total;
    buf[10]= (uint8_t)(m_total>>8);
    buf[11]= (uint8_t)(m_total>>16);
    buf[12]= (uint8_t)(m_total>>24);

    buf[13]= (uint8_t)m_remain;
    buf[14]= (uint8_t)(m_remain>>8);
    buf[15]= (uint8_t)(m_remain>>16);
    buf[16]= (uint8_t)(m_remain>>24);

    for(uint8_t i =0;i<8;i++)buf[17+i]=0;

//    for(uint8_t j=0;j<sizeof(m_nfc._string);j++)//20230525
//    {
//        buf[9+j]= m_nfc._string[j];
//    }
//    for(uint8_t j =0;j<(16-sizeof(m_nfc._string));j++)buf[8+sizeof(m_nfc._string)+j]=0;

    for(uint8_t i=4;i<25;i++)sum_xor=sum_xor^buf[i];

    buf[25]=sum_xor;

    bsp_uart2_tx_transmit(buf,26);
}

//一键写卡
void nfc_OneKeyWriteCardCmd(char *buff,uint8_t blocknum)
{
    uint8_t len = strlen(buff);
    uint8_t buf[26]={0};
    uint8_t sum_xor=0;
    buf[0]=0xaa;
    buf[1]=0xbb;
    buf[2]=0x16;
    buf[6]=WriteCardCmd>>8;
    buf[7]=(uint8_t)WriteCardCmd;
    buf[8]=blocknum;

    for(uint8_t j=0;j<len;j++)//20230525
    {
        buf[9+j]= buff[j];
    }
    for(uint8_t j =0;j<(16-len);j++)buf[8+len+j]=0;

    for(uint8_t i=4;i<25;i++)sum_xor=sum_xor^buf[i];

    buf[25]=sum_xor;

    bsp_uart2_tx_transmit(buf,26);
}


void nfc_initvaluecard_send(void)
{
    uint8_t buf[16]={0};
    buf[0]=0xaa;
    buf[1]=0xbb;
    buf[2]=0x06;
    buf[6]=InitValueCmd>>8;
    buf[7]=(uint8_t)InitValueCmd;
    buf[8]=0x52;
    buf[9]=buf[4]^buf[5]^buf[6]^buf[7]^buf[8];

    bsp_uart2_tx_transmit(buf,10);
}

void nfc_ReadValue_send(void)
{
    uint8_t buf[16]={0};
    buf[0]=0xaa;
    buf[1]=0xbb;
    buf[2]=0x06;
    buf[6]=ReadValueCmd>>8;
    buf[7]=(uint8_t)ReadValueCmd;
    buf[8]=0x01;
    buf[9]=buf[4]^buf[5]^buf[6]^buf[7]^buf[8];

    bsp_uart2_tx_transmit(buf,10);
}

void nfc_ReCharge_send(uint32_t ReChargeValue)
{
    uint8_t buf[16]={0};
    buf[0]=0xaa;
    buf[1]=0xbb;
    buf[2]=0x0A;
    buf[6]=ReChargeCmd>>8;
    buf[7]=(uint8_t)ReChargeCmd;

    buf[8]=0x01;

    buf[9]= ReChargeValue;
    buf[10]=ReChargeValue>>8;
    buf[11]=ReChargeValue>>16;
    buf[12]=ReChargeValue>>24;

    buf[13]=buf[4]^buf[5]^buf[6]^buf[7]^buf[8]^buf[9]^buf[10]^buf[11]^buf[12];

    bsp_uart2_tx_transmit(buf,14);
}

void nfc_Decrease_send(uint32_t DecreaseValue)
{
    uint8_t buf[16]={0};
    buf[0]=0xaa;
    buf[1]=0xbb;
    buf[2]=0x0A;
    buf[6]=DecreaseValueCmd>>8;
    buf[7]=(uint8_t)DecreaseValueCmd;

    buf[8]=0x01;

    buf[9]= DecreaseValue;
    buf[10]=DecreaseValue>>8;
    buf[11]=DecreaseValue>>16;
    buf[12]=DecreaseValue>>24;

    buf[13]=buf[4]^buf[5]^buf[6]^buf[7]^buf[8]^buf[9]^buf[10]^buf[11]^buf[12];

    bsp_uart2_tx_transmit(buf,14);
}
